<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="载体项目" prop="nr">
        <el-input
          v-model="queryParams.nr"
          placeholder="请输入载体项目"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属公司" prop="qygs">
        <el-input
          v-model="queryParams.qygs"
          placeholder="请输入归属公司"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="openAddZtDialog">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="openEditZtDialog(selectedRow)"
        >修改</el-button>
      </el-col> -->
    </el-row>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
      border
      highlight-current-row
      style="margin-bottom: 16px"
      @row-click="row => selectedRow = row"
    >
      <el-table-column prop="pxbh" label="编号" align="center" />
      <el-table-column prop="dlb" label="类型" align="center" />
      <el-table-column prop="nr" label="载体项目" align="center" />
      <el-table-column prop="qygs" label="归属公司名称" align="center" />
      <el-table-column prop="qybs" label="公司标识" align="center" />
      <el-table-column prop="gs" label="是否本公司" align="center">
        <template #default="scope">
          <span>{{ scope.row.gs ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="dflx" label="电费类型" align="center">
        <template #default="scope">
          <span v-if="scope.row.dflx==1">单电表计费</span>
          <span v-if="scope.row.dflx==2">单电表+容量费用</span>
        </template>
      </el-table-column>
      <el-table-column prop="yx" label="有效" align="center">
        <template #default="scope">
          <span>{{ scope.row.yx ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scope">
          <el-button link @click="openEditZtDialog(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑载体弹窗，增加收款人、开户行、人民币收款账号 -->
    <el-dialog :title="ztEditMode ? '编辑载体' : '新增载体'" v-model="ztDialogVisible" width="800px" append-to-body>
      <el-form :model="ztForm" :rules="ztRules" ref="ztFormRef" label-width="150px">
        <el-form-item label="项目名称" prop="nr">
          <el-input v-model="ztForm.nr" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目缩写" prop="nra">
          <el-input v-model="ztForm.nra" placeholder="请输入项目缩写" />
        </el-form-item>
        <el-form-item label="公司名称" prop="qygs">
          <el-input v-model="ztForm.qygs" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司缩写" prop="qybs">
          <el-input v-model="ztForm.qybs" placeholder="请输入公司缩写" />
        </el-form-item>
        <el-form-item label="是否本公司" prop="gs">
          <el-radio-group v-model="ztForm.gs">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
       <el-form-item label="计费类型" prop="dflx">
  <el-radio-group v-model="ztForm.dflx">
    <el-radio :label="1">单电表计费</el-radio>
    <el-radio :label="2">单电表+容量费用</el-radio>
  </el-radio-group>
</el-form-item>
        <el-form-item v-if="ztEditMode" label="有效" prop="yx">
          <el-radio-group v-model="ztForm.yx">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 新增收款信息 -->
        <el-form-item label="收款人" prop="skr">
          <el-input v-model="ztForm.skr" placeholder="请输入收款人" />
        </el-form-item>
        <el-form-item label="开户行" prop="khh">
          <el-input v-model="ztForm.khh" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="人民币收款账号" prop="rmbzh">
          <el-input v-model="ztForm.rmbzh" placeholder="请输入人民币收款账号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="ztDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitZtForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, nextTick } from "vue";
import { selectzt, addZt, updateZt } from "@/api/carrier/ammeter";

const tableData = ref([]);
const loading = ref(true);
const total = ref(0);
const single = ref(true);
const multiple = ref(true);
const selectedRow = ref(null);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nr: undefined,
    qygs: undefined,
    lxbh: 1, // 默认类型编号为1，且不会变动
  },
});
const { queryParams } = toRefs(data);

/** 查询载体列表 */
function getList() {
  loading.value = true;
  // 保证每次查询都带上lxbh: 1
  queryParams.value.lxbh = 1;
  selectzt(queryParams.value).then((response) => {
    tableData.value = response.data?.list || [];
    total.value = response.data?.total || 0;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.lxbh = 1; // 保证搜索时lxbh为1
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    nr: undefined,
    qygs: undefined,
    lxbh: 1, // 保证重置后lxbh为1
  };
  getList();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
  selectedRow.value = selection.length === 1 ? selection[0] : null;
}

// 载体弹窗相关
const ztDialogVisible = ref(false);
const ztEditMode = ref(false); // true为编辑，false为新增
const ztFormRef = ref();
const ztForm = reactive({
  nr: "",
  nra: "",
  dlb: "电费统计", // 类型默认“电费统计”
  qygs: "",
  qybs: "",
  gs: 1,
  dflx: 1, // 默认单电表计费
  yx: 1,
  skr: "",
  khh: "",
  rmbzh: "",
});

// 必填校验规则
const ztRules = {
  nr: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
  nra: [{ required: true, message: "请输入项目缩写", trigger: "blur" }],
  qygs: [{ required: true, message: "请输入公司名称", trigger: "blur" }],
  qybs: [{ required: true, message: "请输入公司缩写", trigger: "blur" }],
  gs: [{ required: true, message: "请选择是否本公司", trigger: "change" }],
  dflx: [{ required: true, message: "请选择计费类型", trigger: "change" }],
  dlb: [{ required: true, message: "请输入类型", trigger: "blur" }],
  yx: [{ required: true, message: "请选择有效状态", trigger: "change" }],
  skr: [{ required: true, message: "请输入收款人", trigger: "blur" }],
  khh: [{ required: true, message: "请输入开户行", trigger: "blur" }],
  rmbzh: [{ required: true, message: "请输入人民币收款账号", trigger: "blur" }],
};

// 新增弹窗
function openAddZtDialog() {
  ztEditMode.value = false;
  Object.assign(ztForm, {
    nr: "",
    nra: "",
    dlb: "电费统计",
    qygs: "",
    qybs: "",
    gs: 1,
   dflx: 1, // 默认单电表计费
    yx: 1,
  });
  ztDialogVisible.value = true;
}

// 编辑弹窗
function openEditZtDialog(row) {
  ztEditMode.value = true;
  Object.assign(ztForm, row);
  ztDialogVisible.value = true;
}

// 提交
function submitZtForm() {
  ztFormRef.value.validate(valid => {
    if (!valid) return;
    if (ztEditMode.value) {
      updateZt({ ...ztForm }).then(() => {
        ztDialogVisible.value = false;
        getList();
      });
    } else {
      addZt({ ...ztForm }).then(() => {
        ztDialogVisible.value = false;
        getList();
      });
    }
  });
}

getList();
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
</style>